package com.lanou.web.controller;

import com.lanou.entity.CatTree;
import com.lanou.entity.TbItemCat;
import com.lanou.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by QuChao on 2018/11/9
 */
@Controller
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    @ResponseBody
    @RequestMapping("/item/cat/list")
    public List<Object> catList(@RequestParam(value = "id", defaultValue = "0") Integer id) {
        List<TbItemCat> tbItemCats = itemCatService.findCatByParentId(id);
        List<Object> list = new ArrayList<>();
        for (TbItemCat tbItemCat : tbItemCats) {
            CatTree catTree = new CatTree();
            catTree.setId(tbItemCat.getId());
            catTree.setText(tbItemCat.getName());
            if (tbItemCat.getIsParent()) {
                catTree.setState("closed");
            } else {
                catTree.setState("open");
            }
            list.add(catTree);
        }
        return list;
    }

}
